package com.yuan.util.object;

import java.io.*;

/**
 * Explain:     [序列化工具类]
 * Date:        [2017/8/17 10:36]
 * Coder:       [YouYuan]
 * Version:     [1.0]
 */
@Deprecated
public class SerializeUtil {

    /**
     * 序列化对象为字节数组
     * @param obj
     * @return
     * @throws IOException
     */
    public static byte[] serialize(Serializable obj) throws IOException {
        ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
        ObjectOutputStream objOutput = new ObjectOutputStream(byteOutput);
        objOutput.writeObject(obj);
        byte[] byteArray = byteOutput.toByteArray();
        byteOutput.close();
        objOutput.close();
        return byteArray;
    }

    /**
     * 反序列化字节数组为对象
     * @param bytes
     * @return 反序列的结果对象,操作异常返回null
     */
    public static Object unserialize(byte[] bytes) {
        try {
            ByteArrayInputStream byteInput = new ByteArrayInputStream(bytes);
            ObjectInputStream objInput = new ObjectInputStream(byteInput);
            Object obj = objInput.readObject();
            byteInput.close();
            objInput.close();
            return obj;
        } catch (Exception e) {
            return null;
        }
    }
}
